/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa;

import entidades.Ensayo;
import entidades.Formulario;
import entidades.OrdenDeTrabajo;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.transaction.UserTransaction;
import jpa.exceptions.NonexistentEntityException;
import jpa.exceptions.RollbackFailureException;

/**
 *
 * @author dvasquez
 */
public class FormularioJpaController {

    @Resource
    private UserTransaction utx = null;
    @PersistenceUnit(unitName = "LepucvWebPU")
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Formulario formulario) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            em.persist(formulario);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("Ocurrió un error al intentar deshacer la operación.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Formulario formulario) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            formulario = em.merge(formulario);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("Ocurrió un error al intentar deshacer la operación.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = formulario.getId();
                if (findFormulario(id) == null) {
                    throw new NonexistentEntityException("El formulario con ID " + id + " ya no existe.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Formulario formulario;
            try {
                formulario = em.getReference(Formulario.class, id);
                formulario.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("El formulario con ID " + id + " ya no existe.", enfe);
            }
            em.remove(formulario);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("Ocurrió un error al intentar deshacer la operación.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Formulario> findFormularioEntities() {
        return findFormularioEntities(true, -1, -1);
    }

    public List<Formulario> findFormularioEntities(int maxResults, int firstResult) {
        return findFormularioEntities(false, maxResults, firstResult);
    }

    private List<Formulario> findFormularioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Formulario as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public List<Formulario> findFormularioByCodigoEnsayo(String parameter) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNamedQuery("Formulario.findByEnsayo");
            q.setParameter("ensayo", parameter);
            return q.getResultList();
        } finally {
            em.close();
        }

    }

    public List<Formulario> findFormularioByArea(String parameter) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNamedQuery("Formulario.findByArea");
            q.setParameter("area", parameter);
            return q.getResultList();
        } finally {
            em.close();
        }

    }

    public List<Formulario> findFormularioByNroOT(OrdenDeTrabajo parameter) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNamedQuery("Formulario.findByNroOT");
            q.setParameter("nroOT", parameter);
            return q.getResultList();
        } finally {
            em.close();
        }

    }

    public List<Formulario> findFormularioByRangoAndUser(Date fechainicio, Date fechatermino, String rut) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNamedQuery("Formulario.findByRangoAndUser");
            q.setParameter("fechainicio", fechainicio);
            q.setParameter("fechatermino", fechatermino);
            q.setParameter("rut", rut);
            return q.getResultList();
        } finally {
            em.close();
        }

    }

    public List<Formulario> findFormularioByRangoUserArea(Date fechainicio, Date fechatermino, String rut, String area) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNamedQuery("Formulario.findByRangoUserArea");
            q.setParameter("fechainicio", fechainicio);
            q.setParameter("fechatermino", fechatermino);
            q.setParameter("rut", rut);
            q.setParameter("area", area);
            return q.getResultList();
        } finally {
            em.close();
        }

    }

    public Formulario findFormulario(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Formulario.class, id);
        } finally {
            em.close();
        }
    }

    public int getFormularioCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Formulario as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
